package sz.szsmk.citizencard.ui.newversion.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.util.Log
import android.view.View
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.FragmentPagerAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.chenenyu.router.annotation.Route
import com.flyco.tablayout.listener.OnTabSelectListener
import com.gyf.immersionbar.ktx.immersionBar
import kotlinx.android.synthetic.main.activity_whereto.*
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.db.StationSearchDb
import sz.szsmk.citizencard.db.StationSearchDbDao
import sz.szsmk.citizencard.ui.activity.IMainActivity
import sz.szsmk.citizencard.ui.newversion.activity.adapter.SearchStationAdapter
import sz.szsmk.citizencard.ui.newversion.activity.adapter.WhereToAdapter
import sz.szsmk.citizencard.ui.newversion.config.Constants
import sz.szsmk.citizencard.util.DateTimeUtil
import java.util.*

/**
 *  author : hyt
 *  date : 2020/9/2
 *  description :
 */
@Route(Constants.WHERETO)
class WhereToActivity : sz.szsmk.citizencard.ui.dzsmkavtivity.AppBaseActivity(),   TextWatcher {

    lateinit var adapter: WhereToAdapter
    lateinit var adapterStation: SearchStationAdapter
    var editStation: Int = 1;//标记输入的是开始 还是结束
    var currentPage: Int = 0;
    lateinit var stationSearchDbDao: StationSearchDbDao;
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_whereto)

        immersionBar {
            reset()
            fitsSystemWindows(true)
            statusBarColor(R.color.white)
            statusBarDarkFont(true)
        }

        //获取数据库中的所有地址信息数据
        stationSearchDbDao = DaggerApplication.getmDaoSession().stationSearchDbDao;
        des_end_search.setFocusable(true);
        des_end_search.setFocusableInTouchMode(true);
        des_end_search.requestFocus();
        des_start_search.addTextChangedListener(this)
        des_end_search.addTextChangedListener(this)

        des_start_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //关闭软键盘
                editStation = 0;
                keyboardClose()
                return@OnEditorActionListener true
            }
            false
        })
        des_end_search.setOnEditorActionListener(TextView.OnEditorActionListener { v, actionId, event ->
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                //关闭软键盘
                editStation = 1;
                keyboardClose()
                return@OnEditorActionListener true
            }
            false
        })

        rv_searchResult_refresh.setOnRefreshListener {
            currentPage = 0
            Log.d("clark", "refresh")
            when (editStation) {
                0 -> {
                }
                1 -> {
                }
            }
        }


        rv_searchResult_refresh.setOnLoadMoreListener {
            when (editStation) {
                0 -> {
                }
                1 -> {
                }
            }
        }


        vp_tempGone.adapter = WhereToViewPagerAdapter(this, supportFragmentManager)

        val mFilter = arrayOf(
                getString(R.string.shortTime),
                getString(R.string.shortStep),
                getString(R.string.shortChange))
        ctl_selectLine.setViewPager(vp_tempGone, mFilter)
        ctl_selectLine.setOnTabSelectListener(object : OnTabSelectListener {
            override fun onTabSelect(position: Int) {
                when (position) {
                    0 -> {
                    }
                    1 -> {
                    }
                    2 -> {
                    }
                }

            }

            override fun onTabReselect(position: Int) {
            }

        })



        adapter = WhereToAdapter(this)
        rv_line.layoutManager = LinearLayoutManager(this)
        rv_line.adapter = adapter



        adapterStation = SearchStationAdapter("local")
        rv_searchResult1.layoutManager = LinearLayoutManager(this)
        rv_searchResult1.adapter = adapterStation
        rv_line.visibility = View.GONE;
        rv_searchResult1.visibility = View.VISIBLE;
        rv_searchResult_refresh.visibility = View.VISIBLE;




        des_end_search.setOnFocusChangeListener { v, hasFocus ->

            //v 发生变化的视图    hasFocus:用来判断视图是否获得了焦点
            if (!hasFocus) {
                Log.d("clark", "hasfocue=" + hasFocus)
                this.runOnUiThread {
                    rv_line.visibility = View.GONE;
                    rv_searchResult1.visibility = View.VISIBLE;
                    rv_searchResult_refresh.visibility = View.VISIBLE;
                };
                editStation = 0
            } else {
                Log.d("clark", "hasfocue2=" + hasFocus)
            }
        }
        des_start_search.setOnFocusChangeListener { v, hasFocus ->

            //v 发生变化的视图    hasFocus:用来判断视图是否获得了焦点
            if (!hasFocus) {
                Log.d("clark", "hasfocus=" + hasFocus)
                this.runOnUiThread {
                    rv_line.visibility = View.GONE;
                    rv_searchResult1.visibility = View.VISIBLE;
                    rv_searchResult_refresh.visibility = View.VISIBLE;
                };
                editStation = 1
            } else {
                Log.d("clark", "hasfocus2=" + hasFocus)
            }
        }

//        goWhere_title.setting_title.setText(getString(R.string.goWhere));
//        goWhere_title.back.setOnClickListener { finish(); }
        trip_search_back.setOnClickListener { finish()} ;
        des_switch.setOnClickListener {
        }
    }



    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data != null) {
        }
    }

    override fun getTopTitle() = null


    override fun afterTextChanged(p0: Editable?) {
        Log.d("clark", "11111111111111")
        if (editStation == 1 && "".equals(des_end_search.text)) {

        }
        if (editStation == 0 && "".equals(des_start_search)) {

        }

    }

    override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        Log.d("clark", "222222222222222")
        this.runOnUiThread {
            rv_line.visibility = View.GONE;
            rv_searchResult1.visibility = View.VISIBLE;
            rv_searchResult_refresh.visibility = View.VISIBLE;
        };
    }

    override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
        Log.d("clark", "333333333333333")
    }

}



class WhereToViewPagerAdapter(private val context: Context, fm: FragmentManager)
    : FragmentPagerAdapter(fm, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {

    override fun getItem(position: Int): Fragment {
        return TAB_FRAGMENT[position]
    }

    override fun getPageTitle(position: Int): CharSequence? {
        return TAB_TITLES[position]
    }

    override fun getCount(): Int {
        return TAB_TITLES.size
    }
}

private val TAB_TITLES = arrayOf(
        "卡券",
        "票",
        "证件"
)

private val TAB_FRAGMENT = arrayOf(
        Fragment(),
        Fragment(),
        Fragment()
)